var mb = new messagebox();

$(document).ready(function() {
    handleMenuMain();
    handleSlides();
    handleLogin();

    /*======= UPDATE AUTO ==========*/
    ajaxUpdateTimer();
    setInterval(function() {
        ajaxUpdateTimer();
    }, 60000);
});

function handleMenuMain() {
    $menuMainItem = $('#menuMain > li, #playerProfileDisplay .profile, #playerProfileDisplay .username');
    $menuMainItem.click(function() {
        $menuMainItem.not(this).removeClass('actived');
        $(this).toggleClass('actived');
        var tab = $(this).data('tab');

        var $menuTabActived = $('.menu-tab[data-tab|="' + tab + '"]');
        $('.menu-tab').not($menuTabActived).hide();
        closeAndScrollMenuMainContent($menuTabActived);
    });
}

function handleLogin() {
    $('#dangnhap').click(function() {
        $.ajax({
            url: URL_LODE + '/View/login.php',
            type: 'POST',
            data: {
                action: 'LOGINPOPUP'
            },
            success: function(data) {
                $('body').append(data);
            }
        });
    });
}

function closeAndScrollMenuMainContent($selector) {
    var isHidden = $selector.is(":hidden");
    $selector.stop(true, true).slideToggle(100).promise().done(function() {
        isHidden ? scrollPage($selector) : '';
    });
}
function scrollPage($page) {
    var otPage = $page.offset().top;
    var body = $('body, html');
    body.stop(true, true).animate({scrollTop: otPage}, '500', 'swing');
}

function handleSlides() {
    $("#mainSlides").bxSlider({
        auto: true,
        autoHover: true
    });
}

function ajaxUpdateTimer() {
    $.ajax({
        url: URL_LODE + '/View/functions.php',
        type: 'GET',
        beforeSend: function() {
        },
        data: {
            action: 'GETCURRENTDATE'
        },
        success: function(data) {
            var currentDate = JSON.parse(data);
            var year = currentDate["Year"];
            var month = currentDate["Month"];
            var day = currentDate["Day"];
            var dayOfWeek = currentDate["DayInWeek"];
            var hour = currentDate["Hour"];
            var minute = currentDate["Minute"];
            var second = currentDate["Second"];
            fillTimerCurrent(hour, minute, second);
            fillDayCurrent(year, month, day, dayOfWeek);
        }
    });
}
function fillTimerCurrent(h, m, s) {
    $('#timeCurrent').find('.h').text(h + ':' + m);
    //$('#timeCurrent').find('.s').text(s);
}
function fillDayCurrent(y, m, d, dOW) {
    var dayofweekName = ["", "", "Thứ Hai", "Thứ Ba", "Thứ Tư", "Thứ Năm", "Thứ Sáu", "Thứ Bảy", "Chủ Nhật"];
    $('#dayCurrent').text('Ngày ' + d + '/' + m + '/' + y + ' (' + dayofweekName[dOW] + ')');
}